/** 
 * Snake.java
 * @author Lex
 * 2007-8-21 ����07:11:31
 */
package nibbles.lex;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;

import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import nibbles.lex.extra.SampleConfiguration;

public class Nibbles extends JFrame {
	private JPanel pane = null;
	private Model model = null;
	private JComponent gamePane = null;
	// =================================
	private int row = 20;
	private int column = 20;
	private int cellSize = 15;

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				Nibbles app = new Nibbles(25, 35, 16);
				app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				app.setLocationRelativeTo(null);
				app.setVisible(true);
			}
		});
	}

	public Nibbles(int row, int column, int cellSize) {
		super();
		this.row = row;
		this.column = column;
		this.cellSize = cellSize;
		initialize();
		this.addKeyListener(new Controller(model, gamePane));
		Dimension min = this.getGamePane().getMinimumSize();
		this.setSize(min.width + 10, min.height + 54);
	}

	private void initialize() {
		this.setSize(300, 200);
		this.setContentPane(getPane());
		this.setTitle("̰����");
	}

	private JPanel getPane() {
		if (pane == null) {
			pane = new JPanel();
			pane.setLayout(new BorderLayout());
			pane.add(this.getGamePane(), BorderLayout.CENTER);
			pane.setBackground(Color.white);
		}
		return pane;
	}

	private JComponent getGamePane() {
		if (null == gamePane) {
			gamePane = new View(this.getModel(), cellSize);
		}
		return gamePane;
	}

	private Model getModel() {
		if (null == model) {
			SampleConfiguration cfg = new SampleConfiguration(row, column);
			model = new Model(cfg, this.row, this.column);
		}
		return model;
	}
}
